import os
import sys
import platform
from typing import Optional

def get_system_info() -> dict:
    """
    获取系统信息
    
    Returns:
        dict: 系统信息
    """
    return {
        'platform': platform.system(),
        'platform_version': platform.version(),
        'architecture': platform.architecture()[0],
        'python_version': sys.version,
        'python_executable': sys.executable
    }

def check_dependencies() -> dict:
    """
    检查项目依赖
    
    Returns:
        dict: 依赖检查结果
    """
    dependencies = {
        'ffmpeg': False,
        'ffmpeg-python': False,
        'tkinter': False
    }
    
    # 检查ffmpeg
    try:
        import subprocess
        result = subprocess.run(['ffmpeg', '-version'], 
                              capture_output=True, text=True)
        dependencies['ffmpeg'] = result.returncode == 0
    except:
        dependencies['ffmpeg'] = False
    
    # 检查ffmpeg-python
    try:
        import ffmpeg
        dependencies['ffmpeg-python'] = True
    except ImportError:
        dependencies['ffmpeg-python'] = False
    
    # 检查tkinter
    try:
        import tkinter
        dependencies['tkinter'] = True
    except ImportError:
        dependencies['tkinter'] = False
    
    return dependencies

def format_duration(seconds: float) -> str:
    """
    格式化时长显示
    
    Args:
        seconds: 秒数
        
    Returns:
        str: 格式化的时长
    """
    if seconds < 60:
        return f"{seconds:.1f}秒"
    elif seconds < 3600:
        minutes = int(seconds // 60)
        secs = seconds % 60
        return f"{minutes}分{secs:.1f}秒"
    else:
        hours = int(seconds // 3600)
        minutes = int((seconds % 3600) // 60)
        secs = seconds % 60
        return f"{hours}小时{minutes}分{secs:.1f}秒"

def safe_filename(filename: str) -> str:
    """
    生成安全的文件名
    
    Args:
        filename: 原始文件名
        
    Returns:
        str: 安全的文件名
    """
    # 移除或替换不安全的字符
    unsafe_chars = '<>:"/\\|?*'
    for char in unsafe_chars:
        filename = filename.replace(char, '_')
    
    # 移除前后空格
    filename = filename.strip()
    
    # 如果文件名为空，使用默认名称
    if not filename:
        filename = "unnamed"
    
    return filename

def get_file_extension(file_path: str) -> str:
    """
    获取文件扩展名
    
    Args:
        file_path: 文件路径
        
    Returns:
        str: 文件扩展名（小写）
    """
    return os.path.splitext(file_path)[1].lower()

def is_valid_path(path: str) -> bool:
    """
    检查路径是否有效
    
    Args:
        path: 路径字符串
        
    Returns:
        bool: 路径是否有效
    """
    try:
        # 尝试规范化路径
        os.path.normpath(path)
        return True
    except:
        return False

def create_backup_path(original_path: str, suffix: str = "_backup") -> str:
    """
    创建备份路径
    
    Args:
        original_path: 原始路径
        suffix: 后缀
        
    Returns:
        str: 备份路径
    """
    base, ext = os.path.splitext(original_path)
    return f"{base}{suffix}{ext}"

def get_relative_path(base_path: str, target_path: str) -> str:
    """
    获取相对路径
    
    Args:
        base_path: 基础路径
        target_path: 目标路径
        
    Returns:
        str: 相对路径
    """
    try:
        return os.path.relpath(target_path, base_path)
    except ValueError:
        return target_path 